/**
 * MessageUtils.java
 * Product:BaseLayer
 * Version:1.0
 * Copyright 2009 by DNE
 * All Rights Reserved.
 */
package com.site.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.site.util.constant.Constants;

/**
 * 国际化消息工具类
 */
public class MessageUtils {
	/**
	 * 取国际化消息
	 * 
	 * @param key
	 * @param params
	 * @param request
	 * @return
	 */
	public static String getMessageString(String key, Object[] params,
			HttpServletRequest request) {
		Message message = getMessage(key, params, request);
		if (message == null) {
			return "";
		}
		return message.get_dspMessage();
	}

	/**
	 * 取国际化消息值
	 * 
	 * @param key
	 * @param params
	 * @param request
	 * @return
	 */
	public static String getDisplay(String key, HttpServletRequest request) {
		Message message = getMessage(key, null, request);
		if (message == null) {
			return "";
		}
		return message.get_dspMessage();
	}

	/**
	 * 取国际化消息
	 * 
	 * @param key
	 * @param params
	 * @param request
	 * @return
	 */
	public static Message getMessage(String key, Object[] params,
			HttpServletRequest request) {
		Map map = (Map) request.getSession().getServletContext()
				.getAttribute(Constants.APPLICATION_COMMON_XML);
		Map messageMap = (Map) map.get(Constants.COMMON_MESSAGE_XML);
		String language = (String) request.getSession().getAttribute(
				Constants.COMMON_LANGUAGE);
		if (StringUtils.isEmpty(language)) {
			language = Constants.LANGUAGE_DEFAULT;
			request.getSession().setAttribute(Constants.COMMON_LANGUAGE,
					language);
		}
		return ((MessageMap) messageMap.get(language)).getMessage(key, params);
	}

	/**
	 * 取国际化消息
	 * 
	 * @param key
	 * @param params
	 * @param request
	 * @return
	 */
	public static List<Code> getCodeList(String name, HttpServletRequest request) {
		Map map = (Map) request.getSession().getServletContext()
				.getAttribute(Constants.APPLICATION_COMMON_XML);
		Map codeMap = (Map) map.get(Constants.COMMON_CODE_XML);
		String language = (String) request.getSession().getAttribute(
				Constants.COMMON_LANGUAGE);
		if (StringUtils.isEmpty(language)) {
			language = Constants.LANGUAGE_DEFAULT;
			request.getSession().setAttribute(Constants.COMMON_LANGUAGE,
					language);
		}
		return ((CodeMap) codeMap.get(language)).getCodeList(name);
	}

	public static String getCodeValue(String name, String key,
			HttpServletRequest request) {
		Map map = (Map) request.getSession().getServletContext()
				.getAttribute(Constants.APPLICATION_COMMON_XML);
		Map codeMap = (Map) map.get(Constants.COMMON_CODE_XML);
		String language = (String) request.getSession().getAttribute(
				Constants.COMMON_LANGUAGE);
		if (StringUtils.isEmpty(language)) {
			language = Constants.LANGUAGE_DEFAULT;
			request.getSession().setAttribute(Constants.COMMON_LANGUAGE,
					language);
		}
		List<Code> codeList = ((CodeMap) codeMap.get(language))
				.getCodeList(name);
		for (Code code : codeList) {
			String key2 = code.getCode();
			if (key2.equals(key))
				return code.getText();
		}
		return null;
	}
	public static String getCodeValue(String name, String key,
			HttpSession session) {
		Map map = (Map) session.getServletContext()
				.getAttribute(Constants.APPLICATION_COMMON_XML);
		Map codeMap = (Map) map.get(Constants.COMMON_CODE_XML);
		String language = (String) session.getAttribute(
				Constants.COMMON_LANGUAGE);
		if (StringUtils.isEmpty(language)) {
			language = Constants.LANGUAGE_DEFAULT;
			session.setAttribute(Constants.COMMON_LANGUAGE,
					language);
		}
		List<Code> codeList = ((CodeMap) codeMap.get(language))
				.getCodeList(name);
		for (Code code : codeList) {
			String key2 = code.getCode();
			if (key2.equals(key))
				return code.getText();
		}
		return null;
	}
	
	public static String getLikeCodeValue(String name, String key,
			HttpSession session) {
		Map map = (Map) session.getServletContext()
				.getAttribute(Constants.APPLICATION_COMMON_XML);
		Map codeMap = (Map) map.get(Constants.COMMON_CODE_XML);
		String language = (String) session.getAttribute(
				Constants.COMMON_LANGUAGE);
		if (StringUtils.isEmpty(language)) {
			language = Constants.LANGUAGE_DEFAULT;
			session.setAttribute(Constants.COMMON_LANGUAGE,
					language);
		}
		List<Code> codeList = ((CodeMap) codeMap.get(language))
				.getCodeList(name);
		for (Code code : codeList) {
			String key2 = code.getCode();
			if (key.indexOf(key2)!=-1)
				return code.getText();
		}
		return null;
	}

	public static Map getCodeListAsMap(String name, HttpServletRequest request) {
		Map map = (Map) request.getSession().getServletContext()
				.getAttribute(Constants.APPLICATION_COMMON_XML);
		Map codeMap = (Map) map.get(Constants.COMMON_CODE_XML);
		String language = (String) request.getSession().getAttribute(
				Constants.COMMON_LANGUAGE);
		if (StringUtils.isEmpty(language)) {
			language = Constants.LANGUAGE_DEFAULT;
			request.getSession().setAttribute(Constants.COMMON_LANGUAGE,
					language);
		}
		List<Code> codeList = ((CodeMap) codeMap.get(language))
				.getCodeList(name);
		Map cm = new HashMap();
		for (Code code : codeList) {
			String key = code.getCode();
			String value = code.getText();
			cm.put(key, value);
		}
		return cm;
	}
	
	/**
	 * 取国际化消息
	 * @param key
	 * @param params
	 * @param request
	 * @return
	 */
	public static String getScreenItem(String key,HttpServletRequest request){
		Map map=(Map) request.getSession().getServletContext().getAttribute(Constants.APPLICATION_COMMON_XML);
		Map screenItemMap=(Map) map.get(Constants.COMMON_SCREENITEM_XML);
		String language=(String) request.getSession().getAttribute(Constants.COMMON_LANGUAGE);
		if(StringUtils.isEmpty(language)){
			language=Constants.LANGUAGE_DEFAULT;
			request.getSession().setAttribute(Constants.COMMON_LANGUAGE, language);
		}
		ScreenItem screenItem=((ScreenItemMap) screenItemMap.get(language)).getScreenItem(key);
		return screenItem==null?key:screenItem.getDspMessage();
	}
}
